package br.org.groupware_workbench.collabElement.coop.recommendMgr.api;

import java.util.List;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.com.caelum.vraptor.view.Results;
import br.org.groupware_workbench.collablet.news.NewsMgrInstance;
import br.org.groupware_workbench.collabletFw.facade.CollabletInstance;
import br.org.groupware_workbench.coreutils.GenericEntity;

@RequestScoped
@Resource
public class RecommendMgrController {

    private final Result result;
    
    public RecommendMgrController(Result result) {
        this.result = result;
    }

    @Post
    @Path(value = "/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}")
    public void save(final CollabletInstance collablet, final RecommendMgrInstance recommendMgr, Recommend recommend) {
        recommendMgr.saveRecommend(recommend);
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(collablet, recommendMgr);
    }

    @Get
    @Path(value = "/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/configure")
    public void listConfigure(final CollabletInstance collablet, RecommendMgrInstance recommendMgr) {
        result.include("numRelated", String.valueOf(recommendMgr.getNumRelated()));
        result.include("collabletInstance", collablet);
        result.include("recommendMgr", recommendMgr);
    }

    @Post
    @Path(value = "/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/updateNumRelated")
    public void setNumRelatedNews(final CollabletInstance collablet, RecommendMgrInstance recommendMgr, final String max) {
        recommendMgr.setNumRelated(Integer.parseInt(max));
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(collablet, recommendMgr);
    }
    
    // TODO deleteAll precisa do parámetro collablet para apagar os registros do collablet nas tabelas 
    @Post
    @Path(value = "/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/calcRecommned")
    public void recalRecommend(final CollabletInstance collablet, RecommendMgrInstance recommendMgr) {
        recommendMgr.deleteAll();
        recommendMgr.calculeAll(((NewsMgrInstance) collablet).list());
        result.use(Results.logic()).redirectTo(RecommendMgrController.class).listConfigure(collablet, recommendMgr);
    }

    @Post
    @Path(value="/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/getRelated/{id}")
    public List<GenericEntity> getRelated(RecommendMgrInstance recommendMgr, GenericEntity genericEntity) {
        recommendMgr.getRelated(genericEntity);
        return null;
    }

    @Post
    @Path(value="/groupware-workbench/{collablet}/recommendMgr/{recommendMgr}/searchRelated/{id}")
    public void generateRecommend(RecommendMgrInstance recommendMgr, CollabletInstance collablet, List<GenericEntity> genericsEntity, GenericEntity genericEntity) {
        recommendMgr.generateRelated(collablet, genericsEntity, genericEntity);
    }
}
